<?php

declare(strict_types=1);

namespace App\Fastapp\Model;

use Hyperf\Database\Model\Events\Creating;
use Hyperf\Database\Model\Relations\HasOne;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property \Carbon\Carbon $created_at
 * @property int $user_id 用户id
 * @property int $wallet_id 钱包id
 * @property int $currency_id 币种id
 * @property string $symbol
 * @property int $type 1充值
 * @property int $object_type 数据类型
 * @property int $object_id 数据id
 * @property string $amount 变动金额
 * @property string $before 变动前
 * @property string $after 变动后
 * @property string $field 账户字段
 * @property int $channel
 */
class AccountWalletLog extends MineModel
{
    const RECHARGE = 1; //充值
    const WITHDRAWAL = 2;//提现
    const USE_BALANCE = 3;//消费

    const OBJ_TYPE_RECHARGE = 1;
    const OBJ_TYPE_WITHDRAWAL = 2;
    const OBJ_TYPE_TASK_LOG = 3;//任务奖励
    const OBJ_TYPE_BLIND_BOX_LOG = 4;//盲盒返利
    const OBJ_TYPE_LOTTO_LOG = 5;
    const OBJ_TYPE_INVITE = 6;//邀请奖励
    const OBJ_TYPE_ADMIN = 7;
    const OBJ_TYPE_CASH_PRIZE_LOG = 8;


    const OBJ_TYPE_WALLET_TRANSFER = 60;//余额转账


    const OBJ_TYPE_GAME = 70;
    const OBJ_TYPE_GAME_BAT = 71;
    const OBJ_TYPE_GAME_AGENT = 73;
    const OBJ_TYPE_AGENT_WAGES = 74;

    public const UPDATED_AT = null;

    public function creating(Creating $event): void
    {
        $event->getModel()->channel = get_user_cache('channel', $event->getModel()->user_id);
    }

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'account_wallet_log';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'created_at', 'user_id', 'wallet_id', 'currency_id', 'symbol', 'type', 'object_type', 'object_id', 'amount', 'before', 'after', 'field', 'channel'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'user_id' => 'integer', 'wallet_id' => 'integer', 'currency_id' => 'integer', 'type' => 'integer', 'object_type' => 'integer', 'object_id' => 'integer', 'amount' => 'decimal:8', 'before' => 'decimal:8', 'after' => 'decimal:8', 'channel' => 'integer'];

    public function currency(): HasOne
    {
        return $this->hasOne(Currency::class, 'symbol', 'symbol');
    }
    public function withdrawal(): HasOne
    {
        return $this->hasOne(AccountWithdrawal::class, 'id', 'object_id');
    }
}
